package com.ycxy.school.express.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ycxy.school.express.pojo.dto.AddressAddNewDTO;
import com.ycxy.school.express.pojo.dto.AddressUpdateDTO;
import com.ycxy.school.express.pojo.dto.FeedbackAddNewDTO;
import com.ycxy.school.express.pojo.dto.FeedbackUpdateDTO;
import com.ycxy.school.express.pojo.vo.AddressListItemVO;
import com.ycxy.school.express.pojo.vo.FeedbackListItemVO;
import com.ycxy.school.express.service.IFeedbackService;
import com.ycxy.school.express.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author jayLuo
 * @Date 2023/1/7 15:28
 * @PackageName:com.ycxy.school.express.controller
 * @ClassName: FeedbackController
 * @Description: TODO
 * @Version 1.0
 */
@Slf4j
@Api(tags = "04. 用户反馈管理模块")
@RestController
@RequestMapping("/feedbacks")
public class FeedbackController {

    @Autowired
    private IFeedbackService feedbackService;

    // http://localhost:9080/feedbacks/add-new
    @ApiOperation("用户提交反馈")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(FeedbackAddNewDTO feedbackAddNewDTO) {
        log.debug("开始处理【添加用户反馈】的请求，参数：{}", feedbackAddNewDTO);
        feedbackService.addNew(feedbackAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/feedbacks/9527/delete
    @ApiOperation("根据id删除用户反馈")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "用户反馈id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@Range(min = 1, message = "删除用户反馈失败，尝试删除的反馈ID无效！")
                                   @PathVariable Long id) {
        log.debug("开始处理【根据id删除用户反馈】的请求，参数：{}", id);
        feedbackService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/feedbacks/9527/update
    @ApiOperation("根据id处理用户反馈")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "用户反馈id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> handleFeedbackById(@PathVariable Long id, FeedbackUpdateDTO feedUpdateDTO) {
        log.debug("开始处理【处理用户反馈】的请求，参数ID：{}, 新数据：{}", id, feedUpdateDTO);
        feedbackService.handleFeedback(id, feedUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/feedbacks/9527/process
    @ApiOperation("将用户反馈标记为处理中")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "用户反馈id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/process")
    public JsonResult<Void> setProcess(@Range(min = 1, message = "标记用户为处理中失败，尝试标记的用户反馈ID无效！")
                                      @PathVariable Long id) {
        log.debug("开始处理【标记用户反馈为处理中】的请求，参数：{}", id);
        feedbackService.setProcess(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/feedbacks
    @ApiOperation("查询用户反馈列表列表")
    @ApiOperationSupport(order = 410)
    @GetMapping("")
    public JsonResult<List<FeedbackListItemVO>> list() {
        log.debug("开始处理【查询用户反馈列表】的请求，无参数");
        List<FeedbackListItemVO> list = feedbackService.list();
        return JsonResult.ok(list);
    }

    // http://localhost:9080/feedbacks/user
    @ApiOperation("根据用户id查询用户反馈列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("/user")
    public JsonResult<List<FeedbackListItemVO>> listByUserId() {
        log.debug("开始处理【根据用户id查询用户反馈列表】的请求");
        List<FeedbackListItemVO> list = feedbackService.listByUserId();
        return JsonResult.ok(list);
    }

}
